package com.chenwang.measure.ui.activity;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.View;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chenwang.measure.R;
import com.chenwang.measure.base.BaseActivity;
import com.chenwang.measure.model.bean.ServerBean;
import com.chenwang.measure.ui.adapter.ServerAdapter;
import com.chenwang.measure.ui.dialog.AddServerDialog;
import com.chenwang.measure.utils.MyToast;
import com.chenwang.measure.utils.SharePreferenceUtils;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import java.lang.reflect.Type;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

//服务器帐号添加
public class ServerAddActivity extends BaseActivity {

    @BindView(R.id.rv)
    RecyclerView mRv;
    private ServerAdapter mAdapter;

    @Override
    public int setLayoutId() {
        return R.layout.activity_server_add;
    }

    @Override
    public void initData() {
        super.initData();
        ButterKnife.bind(this);
    }

    @Override
    protected void onResume() {
        super.onResume();
        initRecyclerView();
    }
   //初始化rv
    private void initRecyclerView(){
        mRv.setLayoutManager(new LinearLayoutManager(this));
        mAdapter = new ServerAdapter(null);
        mRv.setAdapter(mAdapter);
        mAdapter.setOnItemLongClickListener(new BaseQuickAdapter.OnItemLongClickListener() {
            @Override
            public boolean onItemLongClick(BaseQuickAdapter adapter, View view, int position) {
                showBackDialog(adapter.getData(),(ServerBean


                        ) adapter.getData().get(position) );
                return true;
            }
        });
        mAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                List<ServerBean> beans =  adapter.getData();
                Intent intent = new Intent();
                intent.putExtra("data",beans.get(position) );
                setResult(2, intent);
                finish();
            }
        });
        String json = SharePreferenceUtils.getString(this,"server_list" , "");
        if (!TextUtils.isEmpty(json)) {
            Type type = new TypeToken<List<ServerBean>>(){}.getType();
            Gson gson = new Gson();
             List<ServerBean> list = gson.fromJson(json, type);
            mAdapter.setNewData(list);


        }else {
            MyToast.show("没有数据");
        }
    }


    @OnClick(R.id.btn)
    public void onViewClicked() {
        AddServerDialog dialog = new AddServerDialog();
        dialog.setI(new AddServerDialog.I() {
            @Override
            public void success(ServerBean serverBean) {
                mAdapter.add(0,serverBean );
            }
        });
        dialog.show(getSupportFragmentManager(),"" );

    }
    //删除
    private void showBackDialog(final List<ServerBean> list, final ServerBean serverBean){
        final AlertDialog.Builder normalDialog =
                new AlertDialog.Builder(this);
        normalDialog.setTitle("删除");
        normalDialog.setMessage("是否删除");
        normalDialog.setPositiveButton("确定",
                new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        for (int i=0;i<list.size();i++){
                             if (list.get(i).getName().equals(serverBean.getName())) {
                                 list.remove(i);
                                 break;
                             }
                        }
                        Gson gson = new Gson();
                        String json = gson.toJson(list);
                        SharePreferenceUtils.putString(ServerAddActivity.this,"server_list" , json);
                        mAdapter.replaceData(list);


                    }
                });
        normalDialog.setNegativeButton("取消",
                new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.dismiss();
                    }
                });
        normalDialog.show();

    }
}
